/*===========================================================================================    * Date: Sept 2018
	* Purpose: Transform maps created in ArcGiS with modified points into STATA for Placebo
	* MOLA modified points with placebo social borders (change of 5degrees, x 30mts, y 25mts)
	* DATASETS Used:	
		- "$MOLA/ModifiedMOLA/LL_POINTS1881_SocPlacebo_CivPlacebo_NearPlacebo.shp" 
	* DATASETS Created:
		- "ll_point1881_soc_civ_nearplacebo.dta"
===========================================================================================*/

gl A "/Users/`c(username)'/Dropbox/NetworkParish2016/Occupational_Choice/Submission/Final_ReStat/Codes/A_CREATING_SAMPLE"
gl MOLA "/Users/`c(username)'/Dropbox/NetworkParish2016/Occupational_Choice/Submission/Final_ReStat/Codes/MOLA"

cd "$MOLA"

shp2dta using "$MOLA/LL_POINTS1881_SocPlacebo_CivPlacebo_NearPlacebo.shp", database(ll_point1881_soc_civ_nearplacebo) coordinates(londoncoord2) genid(id) replace


use "$MOLA/ll_point1881_soc_civ_nearplacebo.dta", clear

label data "Map_data_Placebo"
cap drop OBJECTID_1
rename OBJECTID_3 objectid_1
label variable id "unique ID"

rename IDMOLA civilplacebo
label variable civilplacebo "Civil placebo parish identifier"
rename IDSocial_1 socialplacebo
label variable socialplacebo "Social placebo parish identifier"
label variable P_NAME  "address"
label variable NAME "census parish"
rename POINT_X longitude 
label variable longitude "Longitude"
rename POINT_Y latitude 
label variable latitude "Latitude"

rename FID_2 ID_socialborderplacebo
label var ID_socialborderplacebo "Unique ID of closest placebo social border (Not IDSocial)"
rename Distance_1 dist_socialborderplacebo
label var dist_socialborderplacebo "Distance to closest placebo social border"
rename IDSocial_L IDSocialplacebo_L
label var IDSocialplacebo_L "IDSocial placebo on the Left"
rename IDSocial_R IDSocialplacebo_R
label var IDSocialplacebo_R "IDSocial placebo on the Right"

order objectid_1 ID_socialborderplacebo IDSocialplacebo_L IDSocialplacebo_R dist_socialborderplacebo socialplacebo
keep objectid_1 ID_socialborderplacebo IDSocialplacebo_L IDSocialplacebo_R dist_socialborderplacebo socialplacebo civilplacebo

save "$MOLA/ll_point1881_soc_civ_nearplacebo", replace



/*
/*/ Distances
forvalues j=1/`=_N'{
	gen distance`j'=0
}
	
forvalues j=1/`=_N'{
	forvalues i=1/`=_N'{
		if `i'~=`j'{
			vincenty latitude[`i'] longitude[`i'] latitude[`j'] longitude[`j'], hav(distance`i'_`j') inkm replace
			replace distance`j'=distance`i'_`j' if _n==`i'
			drop distance`i'_`j'
		}
	}
}

/*/ Shape to dta for buffer data

cd "S:\NetworkParish\Data\Maps\ModifiedMOLA"

shp2dta using POINTS1881_Soc_CALL_BufV1.shp, database(london_buffer) coordinates(buffcoord) genid(id)
*/
